// 本文件用于实现各种功能，如“英汉词典”，“大小写转换”， “Ping”等
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <algorithm>

static std::vector<std::string> func_name = {"translate", "transform", "ping"};             // 功能名
static std::string dic_file = "/home/wyd/linux-testing-repository/vscode/recourse/dic.txt"; // 翻译的文件
static std::string spacer = " ";                                                            // 间隔符

class Dictionary
{
public:
    Dictionary()
    {
        std::ifstream iread(dic_file);
        std::string buffer;
        while (std::getline(iread, buffer))
        {
            size_t pos = buffer.find(spacer);
            std::string word = buffer.substr(0, pos);  // 存储单词
            std::string tran = buffer.substr(pos + 1); // 存储对应单词的解释
            _word_trans[word] = tran;                  // 插入到哈希表中
        }
        iread.close();
    }
    ~Dictionary()
    {
    }

    std::string Find(std::string &word)
    {
        return _word_trans[word] == std::string() ? "unknow" : _word_trans[word];
    }

private:
    std::unordered_map<std::string, std::string> _word_trans; //[banana, 香蕉]
};

std::string Translate(std::string word)
{
    Dictionary dic; // 将词典加载到程序中
    return dic.Find(word);
}

std::string Transform(std::string word) //大写-》小写
{
    std::transform(word.begin(), word.end(), word.begin(), ::tolower);
    return word;
}

std::string Ping()
{
    return "pong";
}